package com.starbaba.utils;

import com.base.common.http.HttpClientUtil;
import com.base.common.http.model.FormData;
import com.base.common.http.model.HttpResult;
import com.base.common.http.model.PostData;
import com.base.common.log.LogService;
import com.base.common.util.StringUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.starbaba.model.utils.ZipUtil;

public class PushUtils {

    private static final Integer SHOW_TYPE = 1;
    private static final Integer NOTIFY_TYPE = 3;
    private static final Integer RESPONSE_TYPE = 2;

    public static <T> void doPush(Long receiver,String prdid,JsonObject jsonObject) {
        String title = null,content=null;
        String icon = "http://img.oneluckydeal.com/common/app_icon.png";
        if(!StringUtils.isEmpty(icon) && "3002".equals(prdid)){
            icon = "http://img.oneluckydeal.com/common/app_icon_us.png";
        }
        String postUrl = "http://xmiles.cn/push_service/common?funid=999&rd="
                + System.currentTimeMillis();
        JsonObject requestjson = new JsonObject();
        requestjson.addProperty("userid", receiver);
        requestjson.addProperty("product_id", prdid);
        JsonObject msgJson = new JsonObject();
        msgJson.addProperty("icon", icon);
        JsonElement jsonElement = jsonObject.get("title");
        if(jsonElement!=null){
            title = jsonElement.getAsString();
        }
        jsonElement = jsonObject.get("content");
        if(jsonElement!=null){
            content = jsonElement.getAsString();
        }
        msgJson.addProperty("title", title);
        msgJson.addProperty("content", content);
        msgJson.addProperty("showType", SHOW_TYPE);
        msgJson.addProperty("notifyType", NOTIFY_TYPE);
        msgJson.addProperty("responseType", RESPONSE_TYPE);
        msgJson.add("responseParams", createResponseParams(jsonObject));
        requestjson.add("msg", msgJson);

        LogService.info("requestData:"+msgJson.toString());
        
        // 请求数据
        FormData formdata = new FormData();
        formdata.addParam("handle", "0");
        formdata.addParam("data", requestjson.toString());
        formdata.addParam("shandle", "1");

        PostData postData = new PostData(formdata);

        try {
            // 请求数据
            HttpResult httpResult = HttpClientUtil.post(postUrl, postData);
            String responsedata = ZipUtil.unCompressData(httpResult.getData());
            System.out.println(responsedata);
        } catch (Exception e) {
            e.printStackTrace();
            LogService.error("推送报错");
        }
    }

    private static JsonObject createResponseParams(JsonObject jsonObject) {
        
        String htmlurl = jsonObject.get("htmlUrl").getAsString();
        String viewTitle = jsonObject.get("webViewTitle").getAsString();
        JsonObject responseParams = new JsonObject();
        responseParams.addProperty("launch", "launch_vc_webView");
        JsonObject launchParams = new JsonObject();
        launchParams.addProperty("title",viewTitle);
        launchParams.addProperty("htmlUrl", htmlurl);
        launchParams.addProperty("withHead", 1);
        launchParams.addProperty("showToolbar", 0);
        launchParams.addProperty("clearTop", 0);
        launchParams.addProperty("canBlockNetworkImg", 1);
        launchParams.addProperty("reloadWhenLogin", 1);
        launchParams.addProperty("takeOverBackPressed", 0);
        launchParams.addProperty("callbackWhenResumeAndPause", 0);
        launchParams.addProperty("showTitle", 1);
        launchParams.addProperty("injectCss", 0);
        launchParams.addProperty("usePost", 1);
        launchParams.add("postData", jsonObject);
        responseParams.add("launchParams", launchParams);
        return responseParams;
    }
}